package com.wyw.learning.study;

/**
 * 选择排序
 *
 * @Author Mr Wu    yewen.wu.china@gmail.com
 * Update History:
 * Author        Time            Content
 */
public class SelectionSort {

    public static void selectionSort (int[] a) {
        int n = a.length;
        for( int i = 0 ; i < n ; i ++ ){
            // 寻找[i, n)区间里的最小值的索引
            int minIndex = i;
            for( int j = i + 1 ; j < n ; j ++ )
                if( a[j] < a[minIndex] )
                    minIndex = j;

            swap( a , i , minIndex);
        }
    }

    private static void swap(int[] arr, int i, int j) {
        int t = arr[i];
        arr[i] = arr[j];
        arr[j] = t;
    }

    public static void main(String[] args) {
        int[] a = new int[]{10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
        selectionSort(a);
        for (int i = 0; i< a.length; i++) {
            System.out.println(a[i]);
        }
    }
}
